Conversation
Monaco ships a Python tokenizer but no language service, so the editor's only suggestions were words already present in the buffer -- typing `np.` or `torch.` offered nothing, which is a rough contrast with the IDE most people are used to. This registers a completion provider backed by a generated dataset: - `scripts/gen_completions.py` introspects the exact modules the grading sandbox injects into every submission (`grading_service/main.py`: torch, nn, F, np, math, Tensor) and emits signatures plus one-line docstring summaries. Following the existing `export_problems.py` / `build_solutions.py` convention, the artifact is committed so contributors don't need to regenerate it; re-run it after a torch or numpy upgrade. - `web/src/lib/pythonCompletions.ts` resolves the receiver left of the dot, including the long forms people type out of habit (`torch.nn.functional.`, `numpy.`). For an unrecognised receiver it falls back to Tensor members, since in these problems nearly every local is a tensor -- and the alternative is other words in the file. Suggestions are suppressed inside strings and comments. 2,528 completions, ~310 KB. The data is imported dynamically so it lands in the same lazy chunk as the editor and stays out of the initial page bundle -- verified against `next build`: the first-load shared chunks are unchanged and the payload is code-split into its own chunk. Word-based suggestions are kept for local variables, with `localityBonus` so nearby names still rank well. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Monaco ships a Python tokenizer, not a language service. The editor's only suggestions come from the built-in word-based provider, i.e. words that already appear in the current buffer. So typing
np.ortorch.offers nothing, and the contrast with the IDE most people arrive from is pretty stark — especially in a tool whose whole premise is "write the implementation yourself".The fix
A completion provider backed by a generated dataset, in two pieces.
scripts/gen_completions.pyintrospects the exact modules the grading sandbox injects into every submission —grading_service/main.pybindstorch,nn,F,np,math,Tensor— and emits labels, kinds, signatures and one-line docstring summaries. Scoping it to the sandbox namespace means the editor can never suggest something the grader would reject.It follows the existing
export_problems.py/build_solutions.pyconvention: the artifact is committed so contributors don't need torch installed to build the web app. Re-run it after upgrading torch or numpy.web/src/lib/pythonCompletions.tsregisters the provider on.:torch.nn.functional.,numpy.,torch.Tensor.all map to the right namespace.Tensormembers. In these problems nearly every local is a tensor, and the alternative is the word-based provider offering other identifiers from the file. This is a heuristic and is commented as one.Word-based suggestions are kept for local variables, with
localityBonusso nearby names still rank well.Bundle size
The dataset is ~310 KB, so it is imported dynamically. It lands in the same lazy chunk as the editor (already
dynamic(..., { ssr: false })) and never reaches the initial page bundle. Verified againstnext build:51-*.js45.5 kB and88b0a408-*.js54.2 kB — unchanged, andgrep logsumexpfinds 0 hits in either/problems/[id]First Load JS: 165 kB, unchangedGzipped over the wire it is ~53 KB, once, cached thereafter.
Testing
tsc --noEmitclean,next buildclean.I also drove the provider headlessly with a stubbed
monacoobject to check the resolution logic rather than just the types — 17 assertions covering each namespace, the dotted aliases, the Tensor fallback, top-level suggestions, and string/comment suppression:Known limitations
x.afterx = model(...)guesses Tensor members. A real language service (Pyright in a web worker) would do better, but that is a much larger change and a much larger bundle.inspect.signaturewith a docstring fallback for C extensions. A handful of torch builtins expose neither and get nodetail.Happy to split the generator and the provider into separate commits, trim the namespaces, or drop the README touch-ups if you'd prefer a tighter diff.